package com.minigame.rpc.network;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class RpcMsgEncoder extends MessageToByteEncoder<RpcMsg> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, RpcMsg rpcMsg, ByteBuf byteBuf) throws Exception {
        int cmd = rpcMsg.getCmd();

        byte[] headDatas = rpcMsg.getHeadDatas();
        byte[] bodyDatas = rpcMsg.getBodyDatas();

        int headLength = 0;
        if(headDatas != null){
            headLength = headDatas.length;
        }
        int bodyLength = 0;
        if(bodyDatas != null){
            bodyLength = bodyDatas.length;
        }
        byteBuf.writeInt(cmd);
        byteBuf.writeInt(headLength);
        byteBuf.writeInt(bodyLength);
        byteBuf.writeBytes(headDatas);
        byteBuf.writeBytes(bodyDatas);
    }
}
